package cn.sanshuilin.kst.system.provider.write.dao;

import cn.sanshuilin.kst.common.utils.LogPrinter;
import cn.sanshuilin.kst.system.provider.bean.Rate;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.List;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2017-12-01 22:02
 * Copyright 2017  by 三水林科技有限公司
 **/
@Repository
public class RateReq extends AbstractJpaDao<Rate> {




    public Object getRatesCount(String strKeyword) {
        long count = 0;
            Query query = this.getRatesQuery(" COUNT(*) ", strKeyword);
        return  query.getSingleResult();
    }


    public List<Rate> getRates(int nPageIndex, int nPageSize, String toMemberID) {

        Query query = getRatesQuery("c", toMemberID);

        query.setFirstResult(nPageIndex * nPageSize);
        query.setMaxResults(nPageSize);
        List<Rate> list = query.getResultList();


        return list;
    }

    private Query getRatesQuery(String strChange, String toMemberID) {
        String hql = "SELECT " + strChange + " FROM Rate c WHERE (c.ToMemberID LIKE :toMemberID ) ";

        hql += " ORDER BY c.PubDate DESC ";

        Query query = entityManager.createNativeQuery(hql);

        query.setParameter("toMemberID", toMemberID);

        LogPrinter.info("----------------------" + query);

        return query;
    }


}
